﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using FaceliftCore;
using FaceliftCore.Attributes;
using FaceliftCore.Attributes.GUI;
using FaceliftCore.Attributes.GUI.Layout;

namespace Facelift.Sandbox
{
    [ViewModel]
    [StackPanelLayout(Orientation = Orientation.Vertical)]
    public class WindowViewModel
    {
        [Label("Ceva Fain Tare:")]
        public string CevaFain { get; set; }

        [Label("tralala")]
        public string Tralla { get; set; }

        public WindowViewModel()
        {
            CevaFain = "Tare";
            Tralla = "pam pam";
        }

        [ButtonHandler("OK")]
        public void btnOk_Click(object sender, EventArgs e)
        {
            MessageBox.Show(string.Format("{0} {1}", CevaFain, Tralla));
        }
    }



    public class MagicSingleton
    {
        internal void OpenTestWindow(WindowViewModel vm)
        {
            Window wnd = new Window();
            wnd.SizeToContent = SizeToContent.WidthAndHeight;
            Panel pnl = null;

            var t = vm.GetType();

            var attributes = t.GetCustomAttributes(typeof(LayoutAttribute), true);

            var layoutAttr = (LayoutAttribute)attributes[0];

            if (layoutAttr.GetType() == typeof(StackPanelLayoutAttribute))
            {
                pnl = new StackPanel();
                (pnl as StackPanel).Orientation = (layoutAttr as StackPanelLayoutAttribute).Orientation;
            }

            var props = t.GetProperties();

            foreach (PropertyInfo x in props)
            {
                StackPanel formItem = new StackPanel();
                formItem.Orientation = Orientation.Horizontal;

                var propAttr = x.GetCustomAttributes();
                var type = x.PropertyType;

                if (type == typeof(string))
                {
                    foreach (var y in propAttr)
                    {
                        if (y.GetType() == typeof(LabelAttribute))
                        {
                            TextBlock l = new TextBlock();
                            l.Text = (y as LabelAttribute).Label;
                            formItem.Children.Add(l);
                        }
                    }

                    TextBox tb = new TextBox();
                    tb.Text = (string)x.GetValue(vm);
                    formItem.Children.Add(tb);
                }

                pnl.Children.Add(formItem);
            }

            var methods = t.GetMethods();

            foreach (MethodInfo x in methods)
            {
                var buttonAttribute = x.GetCustomAttribute(typeof(ButtonHandlerAttribute));
                if (buttonAttribute != null)
                {
                    Button b = new Button();
                    b.Content = (buttonAttribute as ButtonHandlerAttribute).ButtonCaption;
                    b.Click += (e, sender) => x.Invoke(vm, new object[] { e, sender });
                    pnl.Children.Add(b);
                }

            }

            wnd.Content = pnl;



            wnd.ShowDialog();

        }

        internal void ExecuteMagic(WindowViewModel wnd)
        {
            OpenTestWindow(wnd);
        }
    }


    public partial class MainWindow : Window
    {
        private MagicSingleton s = new MagicSingleton();

        public MainWindow()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            WindowViewModel wnd = new WindowViewModel();
            s.ExecuteMagic(wnd);
        }
    }
}
